package summary

import (
	"fmt"

	"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
	"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
	"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
)

func GetChannels(sdk *fabsdk.FabricSDK, orgAdmin, orgName, peerUrl string) ([]string, error) {
	var chlist []string
	chlist = make([]string, 0)
	clientContext := sdk.Context(fabsdk.WithUser(orgAdmin), fabsdk.WithOrg(orgName))
	fmt.Println(clientContext)

	resMgmtClient, err := resmgmt.New(clientContext)
	if err != nil {
		return nil, err
	}
	target := peerUrl
	response, err := resMgmtClient.QueryChannels(
		resmgmt.WithTargetEndpoints(target),
		resmgmt.WithRetry(retry.DefaultResMgmtOpts),
	)
	if err != nil {
		return nil, err
	}

	for _, chl := range response.Channels {
		chlist = append(chlist, chl.ChannelId)
	}
	return chlist, nil
}
